package com.neusoft.training.podcast.service.impl;

import java.sql.Date;
import java.sql.Time;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.neusoft.training.podcast.entity.Video;
import com.neusoft.training.podcast.mapper.CategoryMapper;
import com.neusoft.training.podcast.mapper.VideoMapper;
import com.neusoft.training.podcast.service.VideoService;

@Service
public class VideoServiceImpl implements VideoService{
	
	@Autowired
	private VideoMapper videoMapper;
	
	@Autowired
	private CategoryMapper categoryMapper;
	
	@Override
	public List<Video> findAllVideos() {
		return videoMapper.findAllVideos();
	}
	@Override
	public void deleteVideo(String videoID) {
		videoMapper.deleteVideo(videoID);
	}
	@Override
	public List<Video> findVideoOfAuthor(Integer authorID){
		return videoMapper.findVideoOfAuthor(authorID);
	}
	

	@Override
	public void addVideo(String videoID, String title, Time totalTime, Date uploadTime, Integer size, Integer authorID,
			Integer categoryID, String extendName, String filePath, Integer videoIsDel, String cover) {
		videoMapper.addVideo(videoID, title, totalTime, uploadTime, size, authorID,categoryID, extendName, filePath, videoIsDel, cover);
		
	}
	@Override
	public void saveVideo(Video video) {
		
		videoMapper.saveVideo(video);
		
	}
	@Override
	public int countCategoryVideos(Integer categoryID) {
		
		return videoMapper.countCategoryVideo(categoryID);
	}
	@Override
	public void changeVideoCover(Video video) {
		
		videoMapper.changeVideoCover(video);
		
	}
	@Override
	public Video findVideoByID(String videoID) {
		
		return videoMapper.findVideoByID(videoID);
	}
	@Override
	public void clickVideo(String videoID) {
		
		videoMapper.updateVideoClickCount(videoID);
		
	}
	@Override
	public void likeVideo(String videoID, Integer recommentCount) {
		
		System.out.println(videoID);
		
		videoMapper.updateVideoRecommentCount(videoID, recommentCount);
		
	}
	@Override
	public List<Video> findVideoByName(String title) {
		
		return videoMapper.findVideoByName(title);
	}
	@Override
	public List<Video> showVideosByCategory(String categoryName) {
		
		List<Video> allVideos = videoMapper.findAllVideos();
		for(int i=allVideos.size()-1;i>=0;i--) {

			String videoCategory = categoryMapper.findCategoryIDbyName(allVideos.get(i).getCategoryID());
			
			if(!videoCategory.equals(categoryName)) {
				allVideos.remove(i);
			}
		}
		
		return allVideos;
	}
	@Override
	public List<Video> findVideoByMName(String title) {
		
		return videoMapper.findVideoByMName(title);
	}
	
	@Override
	public List<Video> findFavouriteVideoByID(Integer userID) {
		// TODO Auto-generated method stub
		return videoMapper.findFavouriteVideo(userID);
	}
	@Override
	public List<Video> findCommentVideo(Integer userID) {
		// TODO Auto-generated method stub
		return videoMapper.findCommentVideo(userID);
	}
	

}
